/**************************************************************************
 * @name: C.c
 * @brief: 进程C模拟移动端
 * @date: 2025/02/10
 * @author: boluzs@163.com
 * @note:
 *          1.接受进程B信号
 *          2.管道读取验证码
 *          3.显示验证码
 * @version: 1.0.0
 * @Copyright (c) 2025 by boluzs@163.com, All Rights Reserved.
 **************************************************************************/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#define FIFO_PATH "/tmp/bcfifo"

void get_code(int signum)
{

    // 1.打开有名管道
    int fifo_fd = open(FIFO_PATH, O_RDWR);
    if (fifo_fd == -1)
    {
        fprintf(stderr, "open [%s] error,%s\n", FIFO_PATH, strerror(errno));
        exit(-1);
    }

    // 3.从有名管道中读取数据
    char buf[128] = {0};
    read(fifo_fd, buf, sizeof(buf));
    printf("验证码是%s\n", buf);
}

int main(int argc, char const *argv[])
{
    // 1.监听信号
    signal(SIGUSR1, get_code);
    for (;;)
        ;
    return 0;
}
